package com.situ.gym.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.gym.listener.CourseExcelListener;
import com.situ.gym.mapper.CourseMapper;
import com.situ.gym.pojo.Course;
import com.situ.gym.pojo.query.CourseQuery;
import com.situ.gym.pojo.vo.ChartCountVO;
import com.situ.gym.pojo.vo.CourseExcelVO;
import com.situ.gym.pojo.vo.CourseVO;
import com.situ.gym.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.gym.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public IPage<CourseVO> list(CourseQuery courseQuery) {
        IPage<CourseVO> page  = new Page<>(courseQuery.getPage(), courseQuery.getLimit());
        return courseMapper.list(page, courseQuery);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        courseMapper.deleteAll(ids);
    }

    @Override
    public List<ChartCountVO> selectHotCourse() {
        return courseMapper.selectHotCourse();
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Course> list = courseMapper.selectList(null);
        ArrayList<CourseExcelVO> courseExcelVOList = new ArrayList<>();
        for (Course course : list) {
            CourseExcelVO courseExcelVO = new CourseExcelVO();
            BeanUtils.copyProperties(course, courseExcelVO);
            courseExcelVOList.add(courseExcelVO);
        }
        ExcelUtil.exportExcel(response, courseExcelVOList, CourseExcelVO.class, "course");
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CourseExcelVO.class, new CourseExcelListener(courseMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //加锁解决缓存击穿问题：sync = true
    //synchronize
    @Cacheable(value = "courseCache", key = "#id", sync = true)
    @Override
    public Course selectById(Integer id) {
        System.out.println("CourseServiceImpl.selectById");
        return courseMapper.selectById(id);
    }

    //  2. 缓存所有商品的名字列表
    @Cacheable(value = "courseNameList", key = "'all'")
    public List<String> selectAllCourseNameList() {
        List<String> list = new ArrayList<>();
        list.add("游泳");
        list.add("跑步");
        return list;
    }

    //@CacheEvict(value = "courseCache", key = "#id")
    @Caching(evict = {
            @CacheEvict(value = "courseCache", key = "#id"),
            @CacheEvict(value = "courseNameList", key = "'all'")
    })
    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    /*@Override
    public Course selectById(Integer id) {
        System.out.println("CourseServiceImpl.selectById");
        //1.先去缓存中取
        Course course = (Course) redisTemplate.opsForValue().get("course:" + id);
        //2.缓存中没有
        if (ObjectUtils.isEmpty(course)) {
            //3.查找数据库
            course = courseMapper.selectById(id);
            if (!ObjectUtils.isEmpty(course)) {
                //4.更新到缓存
                redisTemplate.opsForValue().set("course:" + id, course, 30, TimeUnit.DAYS);
            } else {
                //id不存在恶意攻击，缓存空数据
                redisTemplate.opsForValue().set("course:" + id, new Course(), 1, TimeUnit.MINUTES);
            }

        }
        return course;
    }*/

}
